﻿using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhotoZipTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            long quantity = long.Parse(textBox2.Text);
            var fileDialog = new OpenFileDialog();
            var fileSelect = "";
            var result = fileDialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                fileSelect = fileDialog.FileName;
                textBox1.Text = fileSelect;
            }
            if (string.IsNullOrWhiteSpace(fileSelect))
                return;

            CompressionImage(fileSelect, @"e:\MyWork", quantity);
        }

        /// <summary>
        /// 图片转换为byte[]信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Tobyte_Click(object sender, EventArgs e)
        {
            
            var fileDialog = new OpenFileDialog();
            var fileSelect = "";
            byte[] imgbyte;
            var result = fileDialog.ShowDialog(this);
            {
                fileSelect = fileDialog.FileName;
                imgbyte = ImageToByte(fileSelect);
            }
        }

        /// <summary>
        /// 获取图片的byte[]信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {
            string imgFileName = string.Empty;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image|*.jpg;*.jpeg;*.png;*.gif,*.bmp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                imgFileName = ofd.FileName;
                var fileByte = File.ReadAllBytes(ofd.FileName);
            }
        }

        private void btn_factorial_Click(object sender, EventArgs e)
        {
            var mun = textBox1.Text;

            int result = Factorial(int.Parse(mun));
            MessageBox.Show(result.ToString(), "提示信息", MessageBoxButtons.OKCancel);
        }

        #region 方法

        private int Factorial(int mun) // 阶乘
        {
            if (mun == 0 || mun == 1)
            {
                return 1;
            }
            else
            {
                return mun * Factorial(mun - 1);
            }
        }

        private byte[] ImageToByte(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] byteData = new byte[fs.Length];
            fs.Read(byteData, 0, byteData.Length);
            fs.Close();
            return byteData;

        }

        private static void CompressionImage(string imagePath, string targetFoler, long quantily = 90, string fileSuffix = "compress")
        {
            if (!File.Exists(imagePath))
            {
                throw new FileNotFoundException();
            }
            if (!Directory.Exists(targetFoler))
            {
                Directory.CreateDirectory(targetFoler);
            }
            var fileInfo = new FileInfo(imagePath);
            var fileName = fileInfo.Name.Replace(fileInfo.Extension, "");
            var fileFullName = Path.Combine($"{targetFoler}", $"{fileName}_{fileSuffix}{fileInfo.Extension}"); //保存文件命名
            var imageByte = CompressionImages(imagePath, quantily);
            var ms = new MemoryStream(imageByte);
            var image = Image.FromStream(ms);
            image.Save(fileFullName);
            ms.Close();
            ms.Dispose();
            image.Dispose();
        }

        private static byte[] CompressionImages(string imagePath, long quantily)
        {
            using (var fileStream = new FileStream(imagePath, FileMode.Open))
            {
                using (var img = Image.FromStream(fileStream))
                {
                    using (var bitmap = new Bitmap(img))
                    {
                        var codecInfo = GetEncoder(img.RawFormat);
                        var myEncoder = Encoder.Quality;
                        var myEncoderParameters = new EncoderParameters(1);
                        var myEncoderParameter = new EncoderParameter(myEncoder, quantily);
                        myEncoderParameters.Param[0] = myEncoderParameter;
                        using (var ms = new MemoryStream()) 
                        {
                            bitmap.Save(ms, codecInfo, myEncoderParameters);
                            myEncoderParameters.Dispose();
                            myEncoderParameter.Dispose();
                            return ms.ToArray();
                        }
                    }
                }
            }
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            var codecs = ImageCodecInfo.GetImageDecoders();
            return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
        }

        #endregion
    }
}
